package primetest.impl.service;

import java.io.IOException;
import java.io.Serializable;
import java.util.List;

import javax.inject.Inject;

import primetest.dto.BuildingDto;
import primetest.impl.dao.BuildingDao;
import primetest.impl.dao.Transaction;
import primetest.impl.service.BuildingService;

public class BuildingService implements Serializable {



	/**
	 *
	 */
	private static final long serialVersionUID = -2885108918015856569L;
	private Transaction transaction;
	private BuildingDao buildingDao;

	@Inject
	public BuildingService(Transaction transaction,BuildingDao buildingDao){
		this.transaction = transaction;
		this.buildingDao = buildingDao;
	}

	public List<BuildingDto> getBuildingList() throws IOException {
		try{
			transaction.begin();
			List<BuildingDto> buildingList = buildingDao.getAllList(transaction);
			//transaction.commit();
			return buildingList;
		}catch(IOException e){
			transaction.rollback();
			throw e;
		}
	}



	public void addBuilding(BuildingDto buildingDto) throws IOException {
		try{
			transaction.begin();
			buildingDao.addBuilding(transaction, buildingDto);
			transaction.commit();
		}catch(IOException e){
			transaction.rollback();
			throw e;
		}
	}


	public void updateBuilding(BuildingDto buildingDto) throws IOException {
		try{
			transaction.begin();
			buildingDao.updateBuilding(transaction, buildingDto);
			transaction.commit();
		}catch(IOException e){
			transaction.rollback();
			throw e;
		}
	}


	public void deleteBuilding(String buildingSeq) throws IOException {
		try{
			transaction.begin();
			buildingDao.deleteBuilding(transaction, buildingSeq);
			transaction.commit();
		}catch(IOException e){
			transaction.rollback();
			throw e;
		}
	}

	public BuildingDto findBuilding(String buildingSeq) throws IOException {
		try{
			transaction.begin();
			return buildingDao.findBuilding(transaction, buildingSeq);
		}catch(IOException e){
			transaction.rollback();
			throw e;
		}
	}
}
